﻿Imports System.Data.OleDb
Imports System.Xml  'for generating xml
Imports System.ServiceModel.Syndication   'added via Properties System.ServiceModel.Web




Public Class video2rss

    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        'identify that the content of this page is not html type
        Response.Clear()
        Response.ContentType = "application/rss+xml"
        'didn't work at first - had to also add System.ServiceModel to the properties
        Dim myvFeed As SyndicationFeed = New SyndicationFeed("3D Buzz - Projection Mapping", "5 most recently added showreels", New Uri(okURL("artists.aspx")))
        myvFeed.Language = "en-us"      'specify language - English


        Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("3dBuzzDatabase").ConnectionString)
        Dim SqlString As String = "SELECT TOP 5 Video.[VideoId], Video.[Title], Video.[Description], Video.[OriginalFilename] FROM Video ORDER BY Video.[VideoId] DESC"        'last 5 showreels added - might not be most recent activity as not datestamped
        Dim cmd As OleDbCommand = New OleDbCommand(SqlString, oleDbConn)
        cmd.CommandType = CommandType.Text
        oleDbConn.Open()
        Dim dataReader = cmd.ExecuteReader()    'because we are returning results
        ' dataReader.Read() not needed 13 jan 13

        'selected top 6, but displays top 5 - can't be bothere to check the while, probably should be a different loop through as you lose
        'the first one, quicker to find top 6 and display top 5 - sorry!  but it works.
        'sorted 13 jan - removed extra read

        'add data items to an array that will go to the feed
        Dim myVItems As New List(Of SyndicationItem)
        While dataReader.Read()   'whilst still records being returned
            Dim svItem As New SyndicationItem
            svItem.Title = makeRssTxt(dataReader.Item("Title"))
            svItem.Summary = makeRssTxt(dataReader.Item("Description") & dataReader.Item("OriginalFilename"))
            'sItem.Links.Add(dataReader.Item("WebURL"))
            myVItems.Add(svItem)  'add the syndication item to the array
        End While

        myvFeed.Items = myVItems



        Dim vfeedWriter As XmlWriter = XmlWriter.Create(Response.OutputStream)
        Dim vrssFormatter = New Rss20FeedFormatter(myvFeed)         'format to rss2.0

        'flush the data out
        vrssFormatter.WriteTo(vfeedWriter)    'the xml writer object that writes to the output string
        vfeedWriter.Close()
        oleDbConn.Close()
        oleDbConn.Dispose()
    End Sub

    'function from tutor S Bennett video feed
    Private Function okURL(ByVal urlStr As String)
        Return Request.Url.GetLeftPart(UriPartial.Authority) & ResolveUrl(urlStr)
    End Function

    Private Function makeRssTxt(ByVal someString As String) As TextSyndicationContent
        Return TextSyndicationContent.CreatePlaintextContent(someString)

    End Function

    Private Function makeLink(ByVal someLocation As String) As SyndicationLink
        Return SyndicationLink.CreateAlternateLink(New Uri(someLocation))
    End Function
End Class